<?php
class InfoApp extends CommonApp {

	/**
	 * 首页
	 */
	public function indexAct(){
		$table = (My::arr2var($_REQUEST,'table')) ? My::arr2var($_REQUEST,'table') : 'homeuser';
		if(!My::is_table($table))
			$this->ajaxReturn(404,'表名分类有误!');
		$type = My::arr2var($_REQUEST,'type');
				//search
		$search = array();
		$search['where'] = "1 ";
		if($type === '0' || $type === '1')
			$search['where'] .= "AND type={$type} ";
		// $search['where'] .= "AND A.`item`='{$item}' ";
		//publish
		if(My::arr2var($_GET,'publish')==='0'){
			$search['where'] .= "AND publish=0 ";
		}elseif(My::arr2var($_GET,'publish')==1){
			$search['where'] .= "AND publish=1 ";
		}
		//搜索id
		if(My::is_id(My::arr2var($_GET,'id'))){
			$id = $_GET['id'];
			$search['where'] .= "AND `id`='{$id}' ";
		}
		//搜索姓名
		if(My::arr2var($_GET,'username')!=''){
			$username = $_GET['username'];
			$search['where'] .= "AND `username` like '%{$username}%' ";
		}
		if(isset($_GET['order'])){
			$order = $_GET['order'];
			//排序字段为指派，正向排序为：当前用户最前，关闭最后，然后按照id正序排列
			$search['order'] = ($order=='assign') ? "`assign`={$userid} DESC, `assign`=0 ASC,`assign` DESC,`id` DESC" : $search['order'] = "`{$order}` DESC";
			//设置的字段排序与当前cookie排序一样的时候反向排序
			if(isset($_COOKIE['order']) && $search['order']==$_COOKIE['order']){
				//排序字段为指派，反向排序为：关闭最前，当前用户最后，然后按照id反序排列
				$search['order'] = ($order=='assign') ? "`assign`=0 DESC, `assign`={$userid} ASC,`assign` ASC,`editdate` DESC" : "`{$order}` ASC";
			}
			//字段排序存储cookie值一年
			setcookie('order', $search['order'], time()+31536000,'/');
			$this->ajaxReturn(200,'排序成功!');
		} else $search['order'] = isset($_COOKIE['order']) ? $_COOKIE['order'] : '`id` DESC';
		if(isset($_COOKIE['order']))
			$this->assign('order',$_COOKIE['order']);
		$search['page'] = My::arr2var($_REQUEST,'page').(isset($_SESSION['pagesize'])?$_SESSION['pagesize']:'');//用于参与缓存标识
		$search['pagesize'] = isset($_COOKIE['pagesize']) ? $_COOKIE['pagesize'] : '';
		$result=My::getCache($search,array('pre'=>$table));
		if($result==''){
			//page
			$count = DB($table)->where($search['where'])->count();
			$result['page'] = My::page($count,array('pagesize'=>$search['pagesize']));
			//result
			$result['data'] = DB($table)->where($search['where'])->order($search['order'])->limit($result['page']['limits'])->select();
			//echo DB('')->getLastSql();
			if(is_array($result['data']))
				foreach($result['data'] as &$value){
					unset($value['password']);
					$value['params'] = My::decodeJson($value['params']);
					if(isset($value['userid']))
						$value['username'] = DB('homeuser')->where("id={$value['userid']}")->getField('username');
				}
			My::setCache($search,$result,array('pre'=>$table));
		}
		$this->assign('result',$result);
		$this->display("info/{$table}/index");
	}
	
	/**
	 * 首页
	 */
	public function modifyAct(){
		$table = (My::arr2var($_REQUEST,'table')) ? My::arr2var($_REQUEST,'table') : 'homeuser';
		if(!My::is_table($table))
			$this->ajaxReturn(404,'表名分类有误!');
		$type = My::arr2var($_REQUEST,'type');
		$id = I('get.id');
		$data = '';
		if(My::is_id($id)){
			$result = DB($table)->where("id={$id}")->find();
			$result['params'] = My::decodeJson($result['params']);
			if(isset($result['userid']))
				$data = DB('homeuser')->where("id={$result['userid']}")->field('username,sex,email,mobile')->find();
			if($data){
				$result['username'] = $data['username'];
				$result['sex'] = $data['sex'];
				$result['email'] = $data['email'];
				$result['mobile'] = $data['mobile'];
			}
			unset($result['password']);
		}
		$this->assign('result',$result);
		$this->display("info/{$table}/modify");
	}

}